#define _CRT_SECURE_NO_WARNINGS	

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int arr[1001];

void Find_Common(int* arr1, int arr1Size, int* arr2, int arr2Size)
{
    for (int i = 0; i < arr2Size; i++)
    {
        for (int j = 0; j < arr1Size; j++)
        {
            if (arr1[j] == arr2[i])
                arr[arr2[i]]++;
        }
    }
}
int cmp(const void* x, const void* y)
{
    return *((int*)x) > *((int*)y);
}
int* relativeSortArray(int* arr1, int arr1Size, int* arr2, int arr2Size, int* returnSize)
{
    int* newarr = (int*)calloc(arr1Size, sizeof(int));
    *returnSize = arr1Size;
    memset(arr, 0, sizeof(arr));
    Find_Common(arr1, arr1Size, arr2, arr2Size);
    int index = 0;
    for (int i = 0; i < arr2Size; i++)
    {
        while (arr[arr2[i]]--)
        {
            newarr[index++] = arr2[i];
        }
        arr[arr2[i]] = 1;
    }
    qsort(arr1, arr1Size, sizeof(int), cmp);
    for (int i = 0; i < arr1Size; i++)
    {
        if (arr[arr1[i]] == 0)
            newarr[index++] = arr1[i];
    }
    return newarr;
}